package session.stateless;

import entity.LineItem;
import entity.Product;
import entity.SaleInquiry;
import entity.SaleLead;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
public class SaleInquiryBean implements SaleInquiryBeanLocal {
    @PersistenceContext()
    EntityManager em;
    
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    
    public LineItem createLineItem(Product product, Integer quantity) {
            LineItem lineItem = new LineItem();
            
            lineItem.setLineItemQuantity(quantity);
            lineItem.setProduct(product);
            
            em.persist(lineItem);
            em.flush();
            
            return lineItem;
    }
    
   public LineItem createLineItem(Product product, Integer quantity, Double finalPrice) {
            LineItem lineItem = new LineItem();
            
            lineItem.setLineItemQuantity(quantity);
            lineItem.setFinalPrice(finalPrice);
            lineItem.setProduct(product);
            
            em.persist(lineItem);
            em.flush();
            
            return lineItem;
    }

    public Long createSaleInquiry(Long saleLeadId,List<LineItem> lineItems) {
        SaleInquiry saleInquiry = new SaleInquiry();
        
        SaleLead saleLead =em.find(SaleLead.class, saleLeadId);
        if (saleLead ==null)
        {
            System.out.println("Sale lead does not exist");   
            return Long.valueOf(0);
        }
        else
        {
            saleInquiry.setSaleLead(saleLead);               
   
            saleInquiry.setLineItems(lineItems);
            
            Date date = new Date();
            saleInquiry.setSaleInquiryDate(dateFormat.format(date));
            em.persist(saleInquiry);
            em.flush();
            Collection<SaleInquiry> saleInquiries = new ArrayList<SaleInquiry>();
            saleInquiries = saleLead.getSaleInquiries();
            saleInquiries.add(saleInquiry);
            saleLead.setSaleInquiries(saleInquiries);
            em.persist(saleLead);
            em.flush();
            
            return saleInquiry.getId();
        }
    }

    public void updateSaleInquiry(Long saleInquiryId, List<LineItem> lineItems) {
        SaleInquiry saleInquiry =em.find(SaleInquiry.class, saleInquiryId);
        
        if (saleInquiry ==null)
        {
            System.out.println("Sale Inquiry does not exist.");            
        }
        else
        {
            saleInquiry.setLineItems(lineItems);
            em.persist(saleInquiry);
            em.flush();
        }
    }

    public void deleteSaleInquiry(Long saleInquiryId) {
        SaleInquiry saleInquiry = em.find(SaleInquiry.class, saleInquiryId);
        
        if (saleInquiry ==null)
        {
            System.out.println("Sale Inquiry does not exist");
        }
        else
        {
            saleInquiry.getSaleLead().getSaleInquiries().remove(saleInquiry);
            saleInquiry.getLineItems().removeAll(saleInquiry.getLineItems());
            em.remove(saleInquiry);
            em.flush();
        }
    }

    public List<SaleInquiry> getSaleInquiries() {
        Query q=em.createQuery("SELECT c FROM SaleInquiry c");
        return q.getResultList();        
    }
    
    public List<SaleInquiry> getSaleInquiriesBySaleLead(Long saleLeadId) {
        SaleLead saleLead=em.find(SaleLead.class, saleLeadId);
        
        return (List<SaleInquiry>) saleLead.getSaleInquiries();
    }

    public List<Product> getProduct() {
        Query q = em.createQuery("SELECT c FROM Product c");
        
        return q.getResultList();
    }
}
